/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package org.openwork.portlet.skillmanagement.service.impl;

import java.util.Date;
import java.util.List;

import org.openwork.portlet.skillmanagement.RequiredNameException;
import org.openwork.portlet.skillmanagement.model.SkillCategory;
import org.openwork.portlet.skillmanagement.service.base.SkillCategoryLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.ResourceConstants;

/**
 * <a href="SkillCategoryLocalServiceImpl.java.html"><b><i>View Source</i></b>
 * </a>
 *
 * @author samuelgmartinez
 */
public class SkillCategoryLocalServiceImpl
	extends SkillCategoryLocalServiceBaseImpl {
	
	private static final Log log = LogFactoryUtil.getLog(SkillCategoryLocalServiceImpl.class);
		
	@Override
	public void deleteSkillCategory(long id) throws PortalException,
			SystemException {
		SkillCategory category = getSkillCategory(id);
		Group group = category.getGroup();
		
		resourceLocalService.deleteResource(group.getCompanyId(), SkillCategory.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, id);	
		super.deleteSkillCategory(id);
	}

	public SkillCategory updateSkillCategory(long pk, String name, long userId, long groupId) throws PortalException, SystemException {
		SkillCategory skillCategory = getSkillCategory(pk);
		
		skillCategory.setName(name);
		skillCategory.setGroupId(groupId);
		skillCategory.setModifiedDate(new Date());
		skillCategory.setUserId(userId);
		
		validate(name, userId, groupId);
		skillCategory = updateSkillCategory(skillCategory);
		
		return skillCategory;
	}
	
	public SkillCategory addSkillCategory(String name, long userId, long groupId) throws PortalException, SystemException {
		SkillCategory skillCategory = createSkillCategory(counterLocalService.increment());
		
		skillCategory.setName(name);
		skillCategory.setGroupId(groupId);
		skillCategory.setCreateDate(new Date());
		skillCategory.setUserId(userId);
		
		validate(name, userId, groupId);		
		skillCategory = updateSkillCategory(skillCategory);
		
		long companyId = groupLocalService.getGroup(groupId).getCompanyId();		
		resourceLocalService.addResources(companyId, groupId, userId, SkillCategory.class.getName(), skillCategory.getId(), false, false, false);		
		
		return skillCategory;
	}
	
	protected void validate(String name, long userId, long groupId) throws PortalException {
		if(Validator.isNull(name) || StringPool.BLANK.equals(name)) {
			throw new RequiredNameException();
		}		
	}
	
	public List<SkillCategory> search(long groupId) throws SystemException, PortalException {
		return skillCategoryPersistence.findByGroupId(groupId);
	}
	
	public int searchCount(long groupId) throws SystemException, PortalException {
		return skillCategoryPersistence.countByGroupId(groupId);
	}
	
	public List<SkillCategory> search(long[] groupId) throws SystemException, PortalException {
		return null;
//		return skillCategoryPersistence.findByGroupId(groupId);
	}
	
	public int searchCount(long[] groupId) throws SystemException, PortalException {
		return 0;
		//		return skillCategoryPersistence.countByGroupId(groupId);
	}
	
}